package com.apes.fico.finance.model.gl;

import com.apes.fico.masterdata.gl.model.AccountingOperatingChart;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ChartAccount.CONST_TABLE_NAME, indexes = {
        @Index(columnList = "chart_id"),
        @Index(columnList = "voucher_id"),
        @Index(columnList = "voucher_item_id")
})
public class ChartAccount extends BaseModel {
    public static final String CONST_TABLE_NAME = "gl_chart_account";

    @Id
    @Column(name = "id", nullable = false, length = 30)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "科目余额明细帐", padding = 8)
    @Comment("编号")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false)
    @Comment("公司")
    private PartyRole company;

    @ManyToOne()
    @JoinColumn(name = "acc_operating_chart_id", referencedColumnName = "id", nullable = false)
    @Comment("科目")
    private AccountingOperatingChart accountingOperatingChart;

    @Column(name = "year", nullable = false)
    @Comment("会计年度")
    private int year;

    @Column(name = "period", nullable = false)
    @Comment("会计期间")
    private int period;

    @Column(name = "chart_id", nullable = false, length = 60)
    @Comment("科目余额帐")
    private String chartId;

    @Column(name = "auxed")
    @Comment("是否辅助科目")
    private boolean auxed = false;

    @Column(name = "voucher_id", nullable = false, length = 60)
    @Comment("财务凭证编号")
    private String voucherId;

    @Column(name = "voucher_item_id", nullable = false, length = 60)
    @Comment("财务凭证明细编号")
    private String voucherItemId;

    @Column(name = "change_un_debit", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("未记账借方变动金额")
    private double changeUnDebit = 0.0;

    @Column(name = "un_debit", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("未记账借方期末余额")
    private double unDebit = 0;

    @Column(name = "change_un_credit", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("未记账贷方变动金额")
    private double changeUnCredit = 0.0;

    @Column(name = "un_credit", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("未记账贷方期末余额")
    private double unCredit = 0;

    @Column(name = "change_debit", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("借方变动金额")
    private double changeDebit = 0.0;

    @Column(name = "debit", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("借方期末余额")
    private double debit = 0;

    @Column(name = "change_credit", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("贷方变动金额")
    private double changeCredit = 0.0;

    @Column(name = "credit", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("贷方期末余额")
    private double credit = 0;

    @Column(name = "note", nullable = false, length = 256)
    @Comment("备注")
    private String remarks;

    @Version
    @Column(name = "version", columnDefinition = "number(10) default 0")
    @Comment("版本号")
    private int version;

}
